<html>

<head>
	<title>Web activity manager - Visualizza tasks </title>
	<link href="menu/menu.css" rel="stylesheet" type="text/css"/>
	<script src="menu_and_sessiondata.js" type="text/javascript"></script>
	<style>
	.redcircle {
		width: 20px;
		height: 20px;
		background: red;
		-moz-border-radius: 10px;
		-webkit-border-radius: 10px;
		border-radius: 10px;
	}
	.yellowcircle {
		width: 20px;
		height: 20px;
		background: yellow;
		-moz-border-radius: 10px;
		-webkit-border-radius: 10px;
		border-radius: 10px;
	}
	.greencircle {
		width: 20px;
		height: 20px;
		background: green;
		-moz-border-radius: 10px;
		-webkit-border-radius: 10px;
		border-radius: 10px;
	}
	</style>
	<script src="xmlLoader.js" type="text/javascript"></script>
	<script type="text/javascript">
	
		function generateFunctions(){
				
			var link="/get_sessiondata";
			var sessionXML=loadXMLDoc(link);
			
			var currentrole;
			
			currentrole=sessionXML.getElementsByTagName("role")[0].childNodes[0].nodeValue;
			
			var newText="";
			
			if (currentrole=="Project Manager"){
				newText+='<input type="submit" name="function" value="Elimina"/><input type="submit" name="function" value="Completato"/>';
			}else{
				newText+='<input type="submit" name="function" value="Completato"/>';
			}
			document.getElementById("functions").innerHTML=newText;
				
		}
		
		function init(){
		
			loadSessionData();
			generateMenu();
			loadTaskData();
			generateFunctions();
		}
		
			
		
		function loadTaskData(){	// simile a loadTask(), ma considera tutti i campi dell'XML che passa il server
			
			var link="/sintesiTuttiTask";
			var tasksXML=loadXMLDoc(link);
			
			var tasks=tasksXML.getElementsByTagName("elemento");
			var i;
			var newText='<th><td class="listth">Stato</td><td class="listth">Nome</td><td class="listth">Ore totali</td><td class="listth">Ore lavorate</td><td class="listth">Descrizione</td><td class="listth">Scadenza</td></th>';
			
			for(i=0;i<tasks.length;i++){
						id_task=tasksXML.getElementsByTagName("id_task")[i].childNodes[0].nodeValue;
						name=tasksXML.getElementsByTagName("name")[i].childNodes[0].nodeValue;
						total_hours=tasksXML.getElementsByTagName("total_hours")[i].childNodes[0].nodeValue;
						worked_hours=tasksXML.getElementsByTagName("worked_hours")[i].childNodes[0].nodeValue;
						description=tasksXML.getElementsByTagName("description")[i].childNodes[0].nodeValue;
						isCompleted=tasksXML.getElementsByTagName("isCompleted")[i].childNodes[0].nodeValue;
						expdate=tasksXML.getElementsByTagName("expdate")[i].childNodes[0].nodeValue;
						status=generateStatus(total_hours, worked_hours, isCompleted, expdate);
						
						newText+=('<tr><td><input type="radio" name="id_task" value="'+id_task+'"></td><td><center><div class="'+status+'"></center></div><td>'+name+'</td><td>'+total_hours+'</td><td>'+worked_hours+'</td><td>'+description+'</td><td>'+expdate+'</td></tr>');
			}
			document.getElementById("listatab").innerHTML=newText;
			
		}
		
		function dateCompare(date){
			
			var splitted = date.split("/");
			
			
			var date1 = new Date(splitted[2], splitted[1] - 1, splitted[0], 23, 59, 59, 0);
			
			var currdate = new Date();
			return(date1<currdate);
		
		}
		
		function generateStatus(total, worked, iscompleted, expdate){
		
			if(iscompleted=="1"){
				return "greencircle";
			}
			if(dateCompare(expdate)){
				return "redcircle";
			}
			if(parseInt(total)<parseInt(worked)){
				return "redcircle";
			}
			return "yellowcircle";



		}
			
	</script>
	<link href="style/style.css" rel="stylesheet" type="text/css"/>
</head>

<body onload="init()">
	<div class = "header">

		<div class = "headerban">
			<h1><center> WEB ACTIVITY MANAGER </center></h1>
		</div>
		
		<div class = "session_data">
			<table>
				<tr>
					<td><b>Utente: </b></td>
					<td><div id = "current_user"></div>
					</td>
				</tr>
				<tr>
					<td>
						<b>Ruolo: </b></td>
					<td><div id = "current_role" ></div>
					</td>
				</tr>
				<tr>
					<td>
						<b>Progetto: </b></td>
					<td><div id = "current_project"></div>
					</td>
				</tr>
				<tr>
					<td>
						<a href="http://localhost:4444/logout"><input type="button" value="logout"/></a>
					</td>
				</tr>
			</table>
		</div>
	</div>
	
	<!-- MENU -->
	<div class = "menu">
		<ul id="menu">
		</ul>
	</div>
	<br>
	<br>
	
	<div class = "funct_name">
		<h2><center> Visualizza i task di progetto </center></h2>
	</div>
	
	<!-- TABELLA DI VISUALIZZAZIONE TASK, CON OPTION PER ELIMINARLI -->
	<div class = "list">
		<form onSubmit="if(!confirm('Sei sicuro?')){return false;}" action="http://localhost:4444/select_task">
			<fieldset>
				<legend> Lista tasks di progetto </legend>
				<table id = "listatab" border="1">
				</table>
				<br>
				<div id="functions">

				</div>
				
			</fieldset>
			
		</form>
	</div>
		
</body>

</html>